package com.ctgu.computer.service.impl;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.ctgu.computer.domain.dto.AdminAddDTO;
import com.ctgu.computer.domain.dto.AdminLoginDTO;
import com.ctgu.computer.domain.dto.UserLoginDTO;
import com.ctgu.computer.domain.dto.UserSignUpDTO;
import com.ctgu.computer.domain.po.AdminInfo;
import com.ctgu.computer.domain.po.UserInfo;
import com.ctgu.computer.mapper.LoginMapper;
import com.ctgu.computer.prop.ServiceAddressProp;
import com.ctgu.computer.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;


import java.util.UUID;

@Service
@RequiredArgsConstructor

public class LoginServiceImpl implements LoginService {

    private final LoginMapper loginMapper;
    private final FileStorageService fileStorageService;
    private final ServiceAddressProp serviceAddressProp;
    @Override
    public UserInfo login(UserLoginDTO userLoginDTO) {
        userLoginDTO.setPassword(DigestUtils.md5Hex(userLoginDTO.getPassword()));
        return loginMapper.login(userLoginDTO);
    }

    @Override
    public UserInfo signUp(UserSignUpDTO userSignUpDTO)  {
        // 判断用户名是否存在
        UserInfo existedUser = loginMapper.selectByUserName(userSignUpDTO.getUserName());
        // md5密码加密
        userSignUpDTO.setPassword(DigestUtils.md5Hex(userSignUpDTO.getPassword()));
        if(existedUser!=null){
            return null;
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userSignUpDTO,userInfo);
        // 生成用户对应的举报页二维码
        //TODO 这里的服务器地址需要换为具体的举报页面
        byte[] bytes = QrCodeUtil.generatePng(serviceAddressProp.getAddress() + "/sys/user/example?userName=" + userInfo.getUserName(), 300, 300);
        //BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("src/main/resources/static/code.png"));
        // 上传至阿里云
        FileInfo fileInfo = fileStorageService.of(bytes)
                .setSaveFilename("code-" + UUID.randomUUID() + ".png")
                .upload();
        userInfo.setQRCode(fileInfo.getUrl());
        loginMapper.insert(userInfo);
        return userInfo;
    }

    @Override
    public AdminInfo login(AdminLoginDTO adminLoginDTO) {
        adminLoginDTO.setPassword(DigestUtils.md5Hex(adminLoginDTO.getPassword()));
//        adminLoginDTO.setPassword(adminLoginDTO.getPassword());

        return loginMapper.loginA(adminLoginDTO);
    }

    @Override
    public AdminInfo addAdmin(AdminAddDTO adminAddDTO) {
        AdminInfo existInfo = loginMapper.selectByAdminName(adminAddDTO.getAdminName());

        //密码加密
        adminAddDTO.setPassword(DigestUtils.md5Hex(adminAddDTO.getPassword()));

        if(existInfo!=null){
            return null;
        }

        AdminInfo adminInfo = new AdminInfo();

        BeanUtils.copyProperties(adminAddDTO,adminInfo);
        adminInfo.setAdminId(adminAddDTO.getAdminId());
        loginMapper.insertAdmin(adminInfo);
        return null;
    }
}
